//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Exercise.h"
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::CExercise::CExercise (QL::Exercise::TypeEnum type) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phExercise = NULL;
#endif
        QuantLib::Exercise::Type _type = (QuantLib::Exercise::Type)type ;
        _ppExercise = new boost::shared_ptr<QuantLib::Exercise> (new QuantLib::Exercise ( _type ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::CExercise::CExercise (boost::shared_ptr<QuantLib::Exercise>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phExercise = NULL;
#endif
	_ppExercise = &childNative;
    
}
Cephei::QL::CExercise::CExercise (QuantLib::Exercise& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phExercise = NULL;
#endif
	_ppExercise = new boost::shared_ptr<QuantLib::Exercise> (&childNative);
    
    _ExerciseOwner = owner;
    
}

Cephei::QL::CExercise::CExercise (CExercise^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phExercise = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppExercise = new boost::shared_ptr<QuantLib::Exercise> (copy->GetShared());
        
    }
}
Cephei::QL::CExercise::CExercise (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phExercise = NULL;
#endif
	if (!t->IsSubclassOf(CExercise::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::CExercise::CExercise (QuantLib::Handle<QuantLib::Exercise>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phExercise = &childNative;
	_ppExercise = &static_cast<boost::shared_ptr<QuantLib::Exercise>>(childNative.currentLink());
    
    _ExerciseOwner = owner;
}
Cephei::QL::CExercise::CExercise (QuantLib::Handle<QuantLib::Exercise> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phExercise = &childNative;
	_ppExercise = &static_cast<boost::shared_ptr<QuantLib::Exercise>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::CExercise::CExercise (QuantLib::Exercise childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phExercise = NULL;
#endif
	_ppExercise = new boost::shared_ptr<QuantLib::Exercise> (new QuantLib::Exercise (childNative));
    
}
#endif

Cephei::QL::CExercise::~CExercise ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppExercise != NULL)
    {
	    delete _ppExercise;
        _ppExercise = NULL;
    }
}
Cephei::QL::CExercise::!CExercise ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppExercise != NULL)
    {
	    delete _ppExercise;
    }
}
QuantLib::Exercise& Cephei::QL::CExercise::GetReference ()
{
    if (_ppExercise == NULL) throw gcnew NativeNullException ();
	return **_ppExercise;
}
boost::shared_ptr<QuantLib::Exercise>& Cephei::QL::CExercise::GetShared ()
{
    if (_ppExercise == NULL) throw gcnew NativeNullException ();
	return *_ppExercise;
}
QuantLib::Exercise* Cephei::QL::CExercise::GetPointer ()
{
    if (_ppExercise == NULL) throw gcnew NativeNullException ();
	return &**_ppExercise;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Exercise>& Cephei::QL::CExercise::GetHandle ()
{
	if (_phExercise == NULL)
	{
		_phExercise = new Handle<QuantLib::Exercise> (*_ppExercise);
	}
	return *_phExercise;
}
#endif
bool Cephei::QL::CExercise::HasNative () 
{
	return (_ppExercise != NULL);
}

DateTime Cephei::QL::CExercise::Date (UInt64 index)
{
    try
    {
        QuantLib::Size _index = (QuantLib::Size)ValueHelper::Convert (index);
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppExercise)->date ( _index );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<DateTime>^ Cephei::QL::CExercise::Dates::get ()
{
    try
    {
    	std::vector<QuantLib::Date>& _rv = (std::vector<QuantLib::Date>&)(*_ppExercise)->dates ( );   
        Cephei::IVector<DateTime>^ _nrv = gcnew CoVector<DateTime> (gcnew CDateTimeVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::CExercise::LastDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppExercise)->lastDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Exercise::TypeEnum Cephei::QL::CExercise::Type::get ()
{
    try
    {
    	QuantLib::Exercise::Type _rv = (QuantLib::Exercise::Type)(*_ppExercise)->type ( );   
        QL::Exercise::TypeEnum _nrv = (QL::Exercise::TypeEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::IExercise^ Cephei::QL::CExercise_Factory::Create (QL::Exercise::TypeEnum type)
{
    return gcnew CExercise ( type);
}
